package com.bw.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;




/**
 * 
 * <p>Title: LoginController</p>
 * <p>Description: 登陆和退出</p>
 */
@Controller
public class LoginController {
	
	/*用户登陆提交
	 * */ 
	@RequestMapping("/login")
	public String loginsubmit(Model model, HttpServletRequest request)
			throws Exception {

		// shiro在认证过程中出现错误后将异常类路径通过request返回
		String exceptionClassName = (String) request
				.getAttribute("shiroLoginFailure");
		if(exceptionClassName!=null){
			if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
				//throw new CustomException("账号不存在");
				System.out.println("账号不存在");
				model.addAttribute("err", "用户名密码不存在，请重新输入");
				return "login";
			} else if (IncorrectCredentialsException.class.getName().equals(
					exceptionClassName)) {
				//throw new CustomException("用户名/密码错误");
				System.out.println("用户名或密码错误");
			} else if("randomCodeError".equals(exceptionClassName)){
				//throw new CustomException("验证码错误");
				System.out.println("验证码错误");
			} else{
				throw new Exception();//最终在异常处理器生成未知错误
			}
		}
		
		return "login";
		
	}
	
        
 }
	
	
	
   
	

